/**
 *	bbx 
 */
define(function(require, exports, module) {
	"use strict";
	//每一个页面都要引入的工具类
	var CommonTools = require('CommonTools_Core');

	//页面跳转管理类
	var WindowTools = require('WindowTools_Core');
	var accountService = require('Account_Service');
	var baseService = require('Base_Service');
	//每一个页面都要引入的工具类
	// initready 要在所有变量初始化做完毕后
	CommonTools.initReady(initData);
	/**
	 * @description 初始化数据,结合initReady使用
	 * plus情况为plusready
	 * 其它情况为直接初始化
	 */
	function initData() {
		//引入必备文件,下拉刷新依赖于mui与mustache
		CommonTools.importFile([
			'js/libs/mui.min.js'
		], function() {
			console.log("init");
			mui.init({
				statusBarBackground: '#f7f7f7',
				beforeback: function() {
					if(plus) {
						var opener = plus.webview.currentWebview().opener();
						if(opener ==  null){
							WindowTools.createWin(null, "../index.html", null, {});
							console.log('aaaaa');
						}else{
							mui.fire(opener, "mainrefresh");
							plus.webview.currentWebview().close();
						}
					}
				}
			});
			WindowTools.preloadTemplate();
			initLogin();
			//WindowTools.dbClickExit();
		});
	}

	function initLogin() {

		//检查 "登录状态/锁屏状态" 结束
		var loginButton = document.getElementById('login'); //登录按钮
		var accountBox = document.getElementById('account'); //用户名
		var passwordBox = document.getElementById('password'); //密码
		var autoLoginButton = document.getElementById("autoLogin"); //自动登录
		var regButton = document.getElementById('reg'); //注册
		//		var forgetButton = document.getElementById('forgetPassword');//忘记密码
		var type = baseService.getType();
		if(type != null) {
			if(typeof(plus)!='undefined') {
				var main = plus.webview.getWebviewById('main');
				if(main != null) {
					mui.fire(main, "mainrefresh");
				}
			}
			if(type == "Student") {
				WindowTools.createWin('main', "../index.html", null, {});
			} else {
				WindowTools.createWin('main', "../admin/index.html", null, {});
			}
		}
		loginButton.addEventListener('tap', function(event) {
			var loginInfo = {
				UserAccount: accountBox.value,
				UserPassword: passwordBox.value,
			};
			console.log("login");
			if(typeof(plus) != 'undefined') {
				console.log('show loading');
				plus.nativeUI.showWaiting('加载中...');
			}
			accountService.login(loginInfo, function(res) {
				debugger;
				if(typeof(plus)!='undefined') {
					var main = plus.webview.getWebviewById('main');
					if(main != null) {
						mui.fire(main, "mainrefresh");
					}
				}
				var style = {};
				if(res.Type == "Student") {
					WindowTools.createWin('main', "../index.html", null, style);
				} else {
					WindowTools.createWin('main', "../admin/index.html", null, style);
				}

			});

		});

		regButton.addEventListener('tap', function(event) {
			var style = {};
			WindowTools.openWinWithTemplate(null, "./reg.html", null, style);
		}, false);
		//		forgetButton.addEventListener('tap', function(event) {
		//			var style = {};
		//			WindowTools.openWinWithTemplate(null, "./forget_password.html", null, style);
		//		}, false);
		//		closeOtherPage();
	}

	function closeOtherPage() {
		if(typeof(plus)!='undefined') {
			// 获取所有Webview窗口
			var curr = plus.webview.currentWebview();
			var wvs = plus.webview.all();
			for(var i = 0, len = wvs.length; i < len; i++) {
				//关闭除setting页面外的其他页面
				if(wvs[i].getURL() == curr.getURL())
					continue;
				plus.webview.close(wvs[i]);
			}
		} else {
			var style = {};
			WindowTools.openWinWithTemplate(id, url, null, style);
		}
	}
});